local skill = fk.CreateSkill {
  name = "ling__amazing_grace_skill",
}

Fk:loadTranslationTable{
  ["#ling__amazing_grace-choose"] = "令至多两名角色选择获得其中一张牌，其余角色随机获得一张剩余牌",
}

skill:addEffect("cardskill", {
  prompt = "#ling__amazing_grace_skill",
  can_use = Util.GlobalCanUse,
  on_use = function (self, room, cardUseEvent)
    return Util.AoeCardOnUse(self, cardUseEvent.from, cardUseEvent, true)
  end,
  mod_target_filter = Util.TrueFunc,
  on_action = function(self, room, use, finished)
    use.extra_data = use.extra_data or {}
    if not finished then
      local toDisplay = {}
      if use.extra_data.orig_cards then
        toDisplay = use.extra_data.orig_cards
      else
        toDisplay = room:getNCards(#use.tos)
      end
      room:moveCards({
        ids = toDisplay,
        from = room:getCardOwner(toDisplay[1]),
        toArea = Card.Processing,
        moveReason = fk.ReasonPut,
      })

      use.extra_data.ling__amazing_grace_first = {}

      if use.from and not use.from.dead then
        local tos = room:askToChoosePlayers(use.from, {
          min_num = 1,
          max_num = 2,
          targets = use.tos,
          skill_name = skill.name,
          prompt = "#ling__amazing_grace-choose",
          cancelable = true,
        })
        if #tos > 0 then
          room:sortByAction(tos)
          tos = table.reverse(tos)
          local new_tos = {}
          for i = #use.tos, 1, -1 do
            local p = use.tos[i]
            if table.contains(tos, p) then
              table.insertIfNeed(use.extra_data.ling__amazing_grace_first, p.id)
              table.insert(new_tos, p)
              table.remove(use.tos, i)
            end
          end
          for _, p in ipairs(use.tos) do
            table.insert(new_tos, p)
          end
          use.tos = new_tos
        end
      end

      use.extra_data.AGFilled = toDisplay
      use.extra_data.AGResult = {}
    else
      if use.extra_data and use.extra_data.AGFilled then

        local toDiscard = table.filter(use.extra_data.AGFilled, function(id)
          return room:getCardArea(id) == Card.Processing
        end)

        if #toDiscard > 0 then
          room:moveCards({
            ids = toDiscard,
            toArea = Card.DiscardPile,
            moveReason = fk.ReasonPutIntoDiscardPile,
          })
        end
      end

      use.extra_data.AGFilled = nil
    end
  end,
  on_effect = function(self, room, effect)
    local to = effect.to
    if not (effect.extra_data and effect.extra_data.AGFilled) then
      return
    end

    local chosen
    if table.contains(effect.extra_data.ling__amazing_grace_first, to.id) then
      chosen = room:askToChooseCard(to, {
        target = to,
        flag = { card_data = {{ skill.name, effect.extra_data.AGFilled }} },
        skill_name = skill.name,
      })
    else
      chosen = table.random(effect.extra_data.AGFilled)
    end
    table.insert(effect.extra_data.AGResult, {to.id, chosen})
    room:moveCardTo(chosen, Card.PlayerHand, to, fk.ReasonPrey, "amazing_grace_skill", nil, true, to)
    table.removeOne(effect.extra_data.AGFilled, chosen)
  end,
})

return skill
